
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>8.6. 优化 React 组件 · MobX 中文文档</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="devtools.html" />
    
    
    <link rel="prev" href="store.html" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"SangKa","repo":"MobX-Docs-CN","type":"star","size":"small","count":true},{"user":"SangKa","repo":"MobX-Docs-CN","type":"fork","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    1. MobX 介绍
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../intro/overview.html">
            
                <a href="../intro/overview.html">
            
                    
                    1.1. MobX 要点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="../intro/concepts.html">
            
                <a href="../intro/concepts.html">
            
                    
                    1.2. 概念与原则
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../refguide/api.html">
            
                <a href="../refguide/api.html">
            
                    
                    2. API概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    3. observable 相关
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../refguide/observable.html">
            
                <a href="../refguide/observable.html">
            
                    
                    3.1. observable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../refguide/observable-decorator.html">
            
                <a href="../refguide/observable-decorator.html">
            
                    
                    3.2. @observable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../refguide/object.html">
            
                <a href="../refguide/object.html">
            
                    
                    3.3. objects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../refguide/array.html">
            
                <a href="../refguide/array.html">
            
                    
                    3.4. arrays
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../refguide/map.html">
            
                <a href="../refguide/map.html">
            
                    
                    3.5. maps
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../refguide/boxed.html">
            
                <a href="../refguide/boxed.html">
            
                    
                    3.6. boxed values
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../refguide/modifiers.html">
            
                <a href="../refguide/modifiers.html">
            
                    
                    3.7 decorators
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    4. 对 observables 作出响应
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../refguide/computed-decorator.html">
            
                <a href="../refguide/computed-decorator.html">
            
                    
                    4.1. (@)computed
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../refguide/autorun.html">
            
                <a href="../refguide/autorun.html">
            
                    
                    4.2. autorun
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../refguide/when.html">
            
                <a href="../refguide/when.html">
            
                    
                    4.3. when
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../refguide/reaction.html">
            
                <a href="../refguide/reaction.html">
            
                    
                    4.4. reaction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../refguide/observer-component.html">
            
                <a href="../refguide/observer-component.html">
            
                    
                    4.5. (@)observer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="react.html">
            
                <a href="react.html">
            
                    
                    4.6. 理解 MobX 对何作出响应
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    5. 改变 observables
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../refguide/action.html">
            
                <a href="../refguide/action.html">
            
                    
                    5.1. action
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="actions.html">
            
                <a href="actions.html">
            
                    
                    5.2. async actions & flows
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../refguide/object-api.html">
            
                <a href="../refguide/object-api.html">
            
                    
                    5.3. Object api
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    6. 工具函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../refguide/tojson.html">
            
                <a href="../refguide/tojson.html">
            
                    
                    6.1. toJS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../refguide/extend-observable.html">
            
                <a href="../refguide/extend-observable.html">
            
                    
                    6.2. extendObservable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../refguide/extending.html">
            
                <a href="../refguide/extending.html">
            
                    
                    6.3. createAtom
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../refguide/observe.html">
            
                <a href="../refguide/observe.html">
            
                    
                    6.4. intercept & observe
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#frompromise">
            
                    
                    6.5. mobxUtils.fromPromise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#fromresource">
            
                    
                    6.6. mobxUtils.fromResource
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#tostream">
            
                    
                    6.7. mobxUtils.toStream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#fromstream">
            
                    
                    6.8. mobxUtils.fromStream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#now">
            
                    
                    6.9. mobxUtils.now
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../refguide/create-transformer.html">
            
                <a href="../refguide/create-transformer.html">
            
                    
                    6.10. mobxUtils.createTransformer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.11" data-path="../refguide/expr.html">
            
                <a href="../refguide/expr.html">
            
                    
                    6.11. mobxUtils.expr
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.12" data-path="../refguide/api.html">
            
                <a href="../refguide/api.html">
            
                    
                    6.12. complete api overview
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <a target="_blank" href="https://github.com/mobxjs/awesome-mobx#examples">
            
                    
                    7. 博客、 视频及相关项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" >
            
                <span>
            
                    
                    8. 贴士与技巧
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../faq/faq.html">
            
                <a href="../faq/faq.html">
            
                    
                    8.1. 常见问题解答
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="pitfalls.html">
            
                <a href="pitfalls.html">
            
                    
                    8.2. 常见陷阱与最佳实践
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="decorators.html">
            
                <a href="decorators.html">
            
                    
                    8.3. 如何(不)使用装饰器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="trace.html">
            
                <a href="trace.html">
            
                    
                    8.4. 使用 trace 进行调试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="store.html">
            
                <a href="store.html">
            
                    
                    8.5. 定义数据存储
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.8.6" data-path="react-performance.html">
            
                <a href="react-performance.html">
            
                    
                    8.6. 优化 React 组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="devtools.html">
            
                <a href="devtools.html">
            
                    
                    8.7. 开发工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="../refguide/spy.html">
            
                <a href="../refguide/spy.html">
            
                    
                    8.8. spy
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../donating.html">
            
                <a href="../donating.html">
            
                    
                    9. 捐赠
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >8.6. 优化 React 组件</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x4F18;&#x5316;-react-&#x7EC4;&#x4EF6;&#x6E32;&#x67D3;"><a name="&#x4F18;&#x5316;-react-&#x7EC4;&#x4EF6;&#x6E32;&#x67D3;" class="plugin-anchor" href="#&#x4F18;&#x5316;-react-&#x7EC4;&#x4EF6;&#x6E32;&#x67D3;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4F18;&#x5316; React &#x7EC4;&#x4EF6;&#x6E32;&#x67D3;</h1>
<p>MobX &#x975E;&#x5E38;&#x5FEB;&#xFF0C;<a href="https://twitter.com/mweststrate/status/718444275239882753" target="_blank">&#x751A;&#x81F3;&#x6BD4; Redux &#x66F4;&#x5FEB;</a>&#x3002;&#x4F46;&#x672C;&#x7AE0;&#x8282;&#x63D0;&#x4F9B;&#x4E00;&#x4E9B;&#x5C0F;&#x8D34;&#x58EB;&#xFF0C;&#x4EE5;&#x4FBF;&#x5145;&#x5206;&#x5229;&#x7528; React &#x548C; MobX&#x3002;
&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x5927;&#x591A;&#x6570;&#x5C0F;&#x8D34;&#x58EB;&#x90FD;&#x9002;&#x7528;&#x4E8E;&#x666E;&#x901A;&#x7684; React&#xFF0C;&#x800C;&#x975E; MobX &#x4E13;&#x7528;&#x7684;&#x3002;</p>
<h2 id="&#x4F7F;&#x7528;&#x5927;&#x91CF;&#x7684;&#x5C0F;&#x7EC4;&#x4EF6;"><a name="&#x4F7F;&#x7528;&#x5927;&#x91CF;&#x7684;&#x5C0F;&#x7EC4;&#x4EF6;" class="plugin-anchor" href="#&#x4F7F;&#x7528;&#x5927;&#x91CF;&#x7684;&#x5C0F;&#x7EC4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4F7F;&#x7528;&#x5927;&#x91CF;&#x7684;&#x5C0F;&#x7EC4;&#x4EF6;</h2>
<p><code>@observer</code> &#x7EC4;&#x4EF6;&#x4F1A;&#x8FFD;&#x8E2A;&#x5B83;&#x4EEC;&#x4F7F;&#x7528;&#x7684;&#x6240;&#x6709;&#x503C;&#xFF0C;&#x5E76;&#x4E14;&#x5F53;&#x5B83;&#x4EEC;&#x4E2D;&#x7684;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x6539;&#x53D8;&#x65F6;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#x3002;
&#x6240;&#x4EE5;&#x4F60;&#x7684;&#x7EC4;&#x4EF6;&#x8D8A;&#x5C0F;&#xFF0C;&#x5B83;&#x4EEC;&#x9700;&#x8981;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#x4EA7;&#x751F;&#x7684;&#x53D8;&#x5316;&#x5219;&#x8D8A;&#x5C0F;;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x7528;&#x6237;&#x754C;&#x9762;&#x7684;&#x66F4;&#x591A;&#x90E8;&#x5206;&#x5177;&#x5907;&#x5F7C;&#x6B64;&#x72EC;&#x7ACB;&#x6E32;&#x67D3;&#x7684;&#x53EF;&#x80FD;&#x6027;&#x3002;</p>
<h2 id="&#x5728;&#x4E13;&#x7528;&#x7EC4;&#x4EF6;&#x4E2D;&#x6E32;&#x67D3;&#x5217;&#x8868;"><a name="&#x5728;&#x4E13;&#x7528;&#x7EC4;&#x4EF6;&#x4E2D;&#x6E32;&#x67D3;&#x5217;&#x8868;" class="plugin-anchor" href="#&#x5728;&#x4E13;&#x7528;&#x7EC4;&#x4EF6;&#x4E2D;&#x6E32;&#x67D3;&#x5217;&#x8868;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5728;&#x4E13;&#x7528;&#x7EC4;&#x4EF6;&#x4E2D;&#x6E32;&#x67D3;&#x5217;&#x8868;</h2>
<p>&#x8FD9;&#x70B9;&#x5728;&#x6E32;&#x67D3;&#x5927;&#x578B;&#x6570;&#x636E;&#x96C6;&#x5408;&#x65F6;&#x5C24;&#x4E3A;&#x91CD;&#x8981;&#x3002;
React &#x5728;&#x6E32;&#x67D3;&#x5927;&#x578B;&#x6570;&#x636E;&#x96C6;&#x5408;&#x65F6;&#x8868;&#x73B0;&#x975E;&#x5E38;&#x7CDF;&#x7CD5;&#xFF0C;&#x56E0;&#x4E3A;&#x534F;&#x8C03;&#x5668;&#x5FC5;&#x987B;&#x8BC4;&#x4F30;&#x6BCF;&#x4E2A;&#x96C6;&#x5408;&#x53D8;&#x5316;&#x7684;&#x96C6;&#x5408;&#x6240;&#x4EA7;&#x751F;&#x7684;&#x7EC4;&#x4EF6;&#x3002;
&#x56E0;&#x6B64;&#xFF0C;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#x4E13;&#x95E8;&#x7684;&#x7EC4;&#x4EF6;&#x6765;&#x6620;&#x5C04;&#x96C6;&#x5408;&#x5E76;&#x6E32;&#x67D3;&#x8FD9;&#x4E2A;&#x7EC4;&#x4EF6;&#xFF0C;&#x4E14;&#x4E0D;&#x518D;&#x6E32;&#x67D3;&#x5176;&#x4ED6;&#x7EC4;&#x4EF6;:</p>
<p>&#x4E0D;&#x597D;&#x7684;:</p>
<pre><code class="lang-javascript">@observer <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyComponent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
    render() {
        <span class="hljs-keyword">const</span> {todos, user} = <span class="hljs-keyword">this</span>.props;
        <span class="hljs-keyword">return</span> (<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
            {user.name}
            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
                {todos.map(todo =&gt; <span class="hljs-tag">&lt;<span class="hljs-name">TodoView</span> <span class="hljs-attr">todo</span>=<span class="hljs-string">{todo}</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{todo.id}</span> /&gt;</span>)}
            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>)
    }
}
</span></code></pre>
<p>&#x5728;&#x4E0A;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x5F53; <code>user.name</code> &#x6539;&#x53D8;&#x65F6;&#xFF0C;React &#x4F1A;&#x4E0D;&#x5FC5;&#x8981;&#x5730;&#x534F;&#x8C03;&#x6240;&#x6709;&#x7684; TodoView &#x7EC4;&#x4EF6;&#x3002;&#x5C3D;&#x7BA1;TodoView &#x7EC4;&#x4EF6;&#x4E0D;&#x4F1A;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#xFF0C;&#x4F46;&#x662F;&#x534F;&#x8C03;&#x7684;&#x8FC7;&#x7A0B;&#x672C;&#x8EAB;&#x662F;&#x975E;&#x5E38;&#x6602;&#x8D35;&#x7684;&#x3002;</p>
<p>&#x597D;&#x7684;:</p>
<pre><code class="lang-javascript">@observer <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyComponent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
    render() {
        <span class="hljs-keyword">const</span> {todos, user} = <span class="hljs-keyword">this</span>.props;
        <span class="hljs-keyword">return</span> (&lt;div&gt;
            {user.name}
            &lt;TodosView todos={todos} /&gt;
        &lt;/div&gt;)
    }
}

@observer class TodosView extends Component {
    render() {
        const {todos} = this.props;
        return &lt;ul&gt;
            {todos.map(todo =&gt; &lt;TodoView todo={todo} key={todo.id} /&gt;)}
        &lt;/ul&gt;)
    }
}
</code></pre>
<h2 id="&#x4E0D;&#x8981;&#x4F7F;&#x7528;&#x6570;&#x7EC4;&#x7684;&#x7D22;&#x5F15;&#x4F5C;&#x4E3A;-key"><a name="&#x4E0D;&#x8981;&#x4F7F;&#x7528;&#x6570;&#x7EC4;&#x7684;&#x7D22;&#x5F15;&#x4F5C;&#x4E3A;-key" class="plugin-anchor" href="#&#x4E0D;&#x8981;&#x4F7F;&#x7528;&#x6570;&#x7EC4;&#x7684;&#x7D22;&#x5F15;&#x4F5C;&#x4E3A;-key"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E0D;&#x8981;&#x4F7F;&#x7528;&#x6570;&#x7EC4;&#x7684;&#x7D22;&#x5F15;&#x4F5C;&#x4E3A; key</h2>
<p>&#x4E0D;&#x7528;&#x4F7F;&#x7528;&#x6570;&#x7EC4;&#x7D22;&#x5F15;&#x6216;&#x8005;&#x4EFB;&#x4F55;&#x5C06;&#x6765;&#x53EF;&#x80FD;&#x4F1A;&#x6539;&#x53D8;&#x7684;&#x503C;&#x4F5C;&#x4E3A; key &#x3002;&#x5982;&#x679C;&#x9700;&#x8981;&#x7684;&#x8BDD;&#x4E3A;&#x4F60;&#x7684;&#x5BF9;&#x8C61;&#x751F;&#x6210; ids&#x3002;
&#x8FD8;&#x53EF;&#x4EE5;&#x53C2;&#x89C1;&#x8FD9;&#x7BC7; <a href="https://medium.com/@robinpokorny/index-as-a-key-is-an-anti-pattern-e0349aece318" target="_blank">&#x535A;&#x5BA2;</a>&#x3002;</p>
<h2 id="&#x665A;&#x4E00;&#x70B9;&#x4F7F;&#x7528;&#x95F4;&#x63A5;&#x5F15;&#x7528;&#x503C;"><a name="&#x665A;&#x4E00;&#x70B9;&#x4F7F;&#x7528;&#x95F4;&#x63A5;&#x5F15;&#x7528;&#x503C;" class="plugin-anchor" href="#&#x665A;&#x4E00;&#x70B9;&#x4F7F;&#x7528;&#x95F4;&#x63A5;&#x5F15;&#x7528;&#x503C;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x665A;&#x4E00;&#x70B9;&#x4F7F;&#x7528;&#x95F4;&#x63A5;&#x5F15;&#x7528;&#x503C;</h2>
<p>&#x4F7F;&#x7528; <code>mobx-react</code> &#x65F6;&#xFF0C;&#x63A8;&#x8350;&#x5C3D;&#x53EF;&#x80FD;&#x665A;&#x7684;&#x4F7F;&#x7528;&#x95F4;&#x63A5;&#x5F15;&#x7528;&#x503C;&#x3002;
&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x5F53;&#x4F7F;&#x7528; observable &#x95F4;&#x63A5;&#x5F15;&#x7528;&#x503C;&#x65F6; MobX &#x4F1A;&#x81EA;&#x52A8;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#x7EC4;&#x4EF6;&#x3002;
&#x5982;&#x679C;&#x95F4;&#x63A5;&#x5F15;&#x7528;&#x503C;&#x53D1;&#x751F;&#x5728;&#x7EC4;&#x4EF6;&#x6811;&#x7684;&#x5C42;&#x7EA7;&#x8D8A;&#x6DF1;&#xFF0C;&#x90A3;&#x4E48;&#x9700;&#x8981;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#x7684;&#x7EC4;&#x4EF6;&#x5C31;&#x8D8A;&#x5C11;&#x3002;</p>
<p>&#x5FEB;&#x7684;:</p>
<p><code>&lt;DisplayName person={person} /&gt;</code></p>
<p>&#x6162;&#x7684;:</p>
<p><code>&lt;DisplayName name={person.name} /&gt;</code>.</p>
<p>&#x540E;&#x8005;&#x5E76;&#x6CA1;&#x6709;&#x4EC0;&#x4E48;&#x9519;&#xFF0C;&#x4F46;&#x662F;&#x5F53; <code>name</code> &#x5C5E;&#x6027;&#x53D8;&#x5316;&#x65F6;&#xFF0C;&#x7B2C;&#x4E00;&#x79CD;&#x60C5;&#x51B5;&#x53EA;&#x4F1A;&#x89E6;&#x53D1; <code>DisplayName</code> &#x7EC4;&#x4EF6;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#xFF0C;&#x800C;&#x7B2C;&#x4E8C;&#x79CD;&#x60C5;&#x51B5;&#x7EC4;&#x4EF6;&#x7684;&#x62E5;&#x6709;&#x8005;&#x9700;&#x8981;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#xFF0C;&#x5982;&#x679C;&#x7EC4;&#x4EF6;&#x7684;&#x62E5;&#x6709;&#x8005;&#x6E32;&#x67D3;&#x8DB3;&#x591F;&#x5FEB;&#x7684;&#x8BDD;&#xFF0C;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x4E5F;&#x80FD;&#x5F88;&#x597D;&#x7684;&#x8FD0;&#x884C;&#x3002;
&#x3001;
&#x4F60;&#x6216;&#x8BB8;&#x6CE8;&#x610F;&#x5230;&#x4E86;&#xFF0C;&#x4E3A;&#x4E86;&#x83B7;&#x5F97;&#x6700;&#x4F73;&#x7684;&#x6027;&#x80FD;&#xFF0C;&#x4F60;&#x4E0D;&#x5F97;&#x4E0D;&#x521B;&#x5EFA;&#x5927;&#x91CF;&#x5C0F;&#x7684; observer &#x7EC4;&#x4EF6;&#xFF0C;&#x5B83;&#x4EEC;&#x6BCF;&#x4E2A;&#x90FD;&#x7528;&#x6765;&#x6E32;&#x67D3;&#x7279;&#x5B9A;&#x6570;&#x636E;&#x7684;&#x4E0D;&#x540C;&#x90E8;&#x5206;&#xFF0C;&#x4F8B;&#x5982;:</p>
<p><code>const PersonNameDisplayer = observer((props) =&gt; &lt;DisplayName name={props.person.name} /&gt;)</code></p>
<p><code>const CarNameDisplayer = observer((props) =&gt; &lt;DisplayName name={props.car.model} /&gt;)</code></p>
<p><code>const ManufacturerNameDisplayer = observer((props) =&gt; &lt;DisplayName name={props.car.manufacturer.name} /&gt;)</code></p>
<p>&#x8FD9;&#x662F;&#x4E00;&#x79CD;&#x6709;&#x6548;&#x7684;&#x9009;&#x9879;&#xFF0C;&#x4F46;&#x5982;&#x679C;&#x6570;&#x636E;&#x6A21;&#x578B;&#x6BD4;&#x8F83;&#x5E9E;&#x5927;&#x7684;&#x8BDD;&#xFF0C;&#x8FD9;&#x4F1A;&#x53D8;&#x5F97;&#x5197;&#x957F;&#x3002;&#x53E6;&#x5916;&#x4E00;&#x79CD;&#x9009;&#x62E9;&#x662F;&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x6765;&#x8FD4;&#x56DE;&#x60F3;&#x8981;&#x6E32;&#x67D3; <code>*Displayer</code> &#x7684;&#x6570;&#x636E;:</p>
<p><code>const GenericNameDisplayer = observer((props) =&gt; &lt;DisplayName name={props.getNameTracked()} /&gt;)</code></p>
<p>&#x7136;&#x540E;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7C7B;&#x4F3C;&#x8FD9;&#x6837;&#x7684;&#x7EC4;&#x4EF6;:</p>
<pre><code class="lang-javascript">render() {
  <span class="hljs-keyword">const</span> { person, car } = <span class="hljs-keyword">this</span>.props;
  <span class="hljs-keyword">return</span> (
    &lt;&gt;
      &lt;GenericNameDisplayer getNameTracked={() =&gt; person.name} /&gt;
      &lt;GenericNameDisplayer getNameTracked={car.getModelTracked} /&gt;
      &lt;GenericNameDisplayer getNameTracked={this.getManufacturerNameTracked} /&gt;
    &lt;/&gt;
  );
}

getManufacturerNameTracked = () =&gt; this.props.car.manufacturer.name;

...
class Car {
  @observable model
  getModelTracked = () =&gt; this.model
}
</code></pre>
<p>&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x5141;&#x8BB8; <code>GenericNameDisplayer</code> &#x6E32;&#x67D3;&#x4EFB;&#x4F55;&#x540D;&#x79F0;&#x7684;&#x7EC4;&#x4EF6;&#xFF0C;&#x4ECE;&#x800C;&#x6574;&#x4E2A;&#x5E94;&#x7528;&#x4E2D;&#x590D;&#x7528;&#x3002;&#x73B0;&#x5728;&#xFF0C;&#x8FD8;&#x9700;&#x8981;&#x89E3;&#x51B3;&#x7684;&#x662F;&#x8FD9;&#x4E9B;&#x51FD;&#x6570;&#x7684;&#x653E;&#x7F6E;&#x95EE;&#x9898;: &#x793A;&#x4F8B;&#x4E2D;&#x5C55;&#x793A;&#x4E86;&#x4E09;&#x79CD;&#x53EF;&#x80FD;&#x6027;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5728; render &#x65B9;&#x6CD5;&#x91CC;&#x521B;&#x5EFA;&#x51FD;&#x6570; (&#x8FD9;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x597D;&#x7684;&#x505A;&#x6CD5;)&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5C06;&#x51FD;&#x6570;&#x653E;&#x7F6E;&#x5728;&#x7EC4;&#x4EF6;&#x4E2D; (<code>getManufacturerNameTracked</code>)&#xFF0C;&#x6216;&#x8005;&#x5C06;&#x51FD;&#x6570;&#x76F4;&#x63A5;&#x653E;&#x5728;&#x5305;&#x542B;&#x6570;&#x636E;&#x7684;&#x5BF9;&#x8C61;&#x4E4B;&#x4E2D; (<code>getModelTracked</code>)&#x3002;</p>
<h2 id="&#x5C3D;&#x65E9;&#x7ED1;&#x5B9A;&#x51FD;&#x6570;"><a name="&#x5C3D;&#x65E9;&#x7ED1;&#x5B9A;&#x51FD;&#x6570;" class="plugin-anchor" href="#&#x5C3D;&#x65E9;&#x7ED1;&#x5B9A;&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5C3D;&#x65E9;&#x7ED1;&#x5B9A;&#x51FD;&#x6570;</h2>
<p>&#x6B64;&#x8D34;&#x58EB;&#x9002;&#x7528;&#x4E8E;&#x666E;&#x901A;&#x7684; React &#x548C;&#x7279;&#x522B;&#x662F;&#x4F7F;&#x7528;&#x4E86; <code>PureRenderMixin</code> &#x7684;&#x5E93;&#xFF0C;&#x5C3D;&#x91CF;&#x907F;&#x514D;&#x5728; render &#x65B9;&#x6CD5;&#x4E2D;&#x521B;&#x5EFA;&#x65B0;&#x7684;&#x95ED;&#x5305;&#x3002;</p>
<p>&#x8FD8;&#x53EF;&#x53C2;&#x89C1;&#x4E00;&#x4E0B;&#x8D44;&#x6E90;:</p>
<ul>
<li><a href="https://facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#autobinding" target="_blank">&#x4F7F;&#x7528;&#x5C5E;&#x6027;&#x521D;&#x59CB;&#x5316;&#x7A0B;&#x5E8F;&#x8FDB;&#x884C;&#x81EA;&#x52A8;&#x7ED1;&#x5B9A;</a></li>
<li><a href="https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md" target="_blank">&#x7528;&#x4E8E; no-bind &#x7684; ESLint &#x89C4;&#x5219;</a></li>
</ul>
<p>&#x4E0D;&#x597D;&#x7684;:</p>
<pre><code class="lang-javascript">render() {
    <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">MyWidget</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> { alert(&apos;hi&apos;) }} /&gt;
}
</span></code></pre>
<p>&#x597D;&#x7684;:</p>
<pre><code class="lang-javascript">render() {
    <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">MyWidget</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.handleClick}</span> /&gt;</span>
}

handleClick = () =&gt; {
    alert(&apos;hi&apos;)
}
</span></code></pre>
<p>&#x4E0D;&#x597D;&#x7684;&#x90A3;&#x4E2A;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C; <code>MyWidget</code> &#x91CC;&#x4F7F;&#x7528;&#x7684; <code>PureRenderMixin</code> &#x4E2D;&#x7684; <code>shouldComponent</code> &#x7684;&#x8FD4;&#x56DE;&#x503C;&#x6C38;&#x8FDC;&#x662F; false&#xFF0C;&#x56E0;&#x4E3A;&#x6BCF;&#x5F53;&#x7236;&#x7EC4;&#x4EF6;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#x65F6;&#x4F60;&#x4F20;&#x9012;&#x7684;&#x90FD;&#x662F;&#x4E00;&#x4E2A;&#x65B0;&#x51FD;&#x6570;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="store.html" class="navigation navigation-prev " aria-label="Previous page: 8.5. 定义数据存储">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="devtools.html" class="navigation navigation-next " aria-label="Next page: 8.7. 开发工具">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"8.6. 优化 React 组件","level":"1.8.6","depth":2,"next":{"title":"8.7. 开发工具","level":"1.8.7","depth":2,"path":"best/devtools.md","ref":"best/devtools.md","articles":[]},"previous":{"title":"8.5. 定义数据存储","level":"1.8.5","depth":2,"path":"best/store.md","ref":"best/store.md","articles":[]},"dir":"ltr"},"config":{"plugins":["github","edit-link","anchors","copy-code-button","github-buttons"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://github.com/SangKa/mobx-docs-cn"},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"github-buttons":{"buttons":[{"user":"SangKa","repo":"MobX-Docs-CN","type":"star","size":"small","count":true},{"user":"SangKa","repo":"MobX-Docs-CN","type":"fork","size":"small","count":true}]},"copy-code-button":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"edit-link":{"label":"完善文档","base":"https://github.com/SangKa/mobx-docs-cn/tree/master/docs"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"MobX 中文文档","gitbook":"3.2.2"},"file":{"path":"best/react-performance.md","mtime":"2018-09-18T13:06:39.936Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2018-09-18T13:06:52.064Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

